<template>
	<div class="user">
		<div class="common-form">新增活动会场</div>
		<div class="product-content">
			<el-form ref="form" :model="form" :rules="formRules" label-width="150px">
				<Basic></Basic>
			</el-form>
			<!--提交-->
			<div class="common-button-wrapper">
				<el-button type="info" @click="gotoBack">返回</el-button>
				<el-button type="primary" @click="onSubmit" :loading="loading">提交</el-button>
			</div>
		</div>
	</div>
</template>
<script>
	import InvitationGiftApi from "@/api/invitationgift.js";
	import Basic from "./part/Basic.vue"
	export default {
		components: {
			Basic
		},
		data() {
			return {
				form: {
					name: "",
					invCondition: 0,
					startTime: "",
					endTime: "",
					imageId: "",
					filePath: "",
					ruleId: "",
					ruleFilePath: "",
					invImageId: "",
					invImagePath: "",
					isShow: 1,
					shareImageId: "",
					shareTitle: "",
					shareDesc: "",
					invType: 0,
					invMember: 0,
					invConsumption: 0,
					/*奖励列表*/
					rewardData: [{
						invitationNum: 1,
						isCoupon: false,
						point: 0,
						isPoint: false,
						couponIds: "",
						couponList: [],
						isBalance: false,
						balance: "",
					}],
					memberReward: {
						invitationNum: 1,
						isCoupon: false,
						point: 0,
						isPoint: false,
						couponIds: "",
						couponList: [],
						isBalance: false,
						balance: "",
					},
					consumptionReward: {
						invitationNum: 1,
						isCoupon: false,
						point: 0,
						isPoint: false,
						couponIds: "",
						couponList: [],
						isBalance: false,
						balance: "",
					},
					invRule: ''
				},
				loading: false,
				formRules: {
					imageId: [{
						required: true,
						message: "请上传背景图",
						trigger: "blur",
					}, ],
					shareImageId: [{
						required: true,
						message: "请上传分享图",
						trigger: "blur",
					}, ],
				},
			};
		},
		provide: function() {
			return {
				form: this.form,
			};
		},
		created() {},
		methods: {
			/*提交表单*/
			onSubmit() {
				let self = this;
				if (self.form.rewardData.length == 0) {
					ElMessage.error("请添加奖项");
					return;
				}
				let form = self.form;
				form.reward = self.form.rewardData;
				self.$refs.form.validate((valid) => {
					if (valid) {
						self.loading = true;
						InvitationGiftApi.addInvitation(form, true)
							.then((data) => {
								self.loading = false;
								if (data.code == 1) {
									ElMessage({
										message: data.msg,
										type: "success",
									});
									self.$router.push("/plus/invitation/index");
								} else {
									self.loading = false;
								}
							})
							.catch((error) => {
								self.loading = false;
							});
					}
				});
			},
			/*返回上一页面*/
			gotoBack() {
				this.$router.back(-1);
			},
		},
	};
</script>
<style>
	.reward_box {
		border: 1px solid #409eff;
		border-radius: 10px;
		padding: 20px;
	}
</style>